package com.rainng.coursesystem.manager.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rainng.coursesystem.controller.admin.vo.SysGroupReqDTO;
import com.rainng.coursesystem.controller.admin.vo.SysUserQueryReqDTO;
import com.rainng.coursesystem.controller.admin.vo.SysUserResDTO;
import com.rainng.coursesystem.dao.AdminDAO;
import com.rainng.coursesystem.dao.RoleDAO;
import com.rainng.coursesystem.dao.UserGroupDAO;
import com.rainng.coursesystem.dao.UserRoleDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.entity.AdminEntity;
import com.rainng.coursesystem.model.entity.RoleEntity;
import com.rainng.coursesystem.model.entity.UserGroupEntity;
import com.rainng.coursesystem.model.entity.UserRoleEntity;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class AdminManager extends BaseManager {
    private final AdminDAO adminDAO;
    private final RoleDAO roleDAO;
    private final UserRoleDAO userRoleDAO;
    private final UserGroupDAO userGroupDAO;

    public AdminManager(AdminDAO adminDAO, RoleDAO roleDAO, UserRoleDAO userRoleDAO, UserGroupDAO userGroupDAO) {
        this.adminDAO = adminDAO;
        this.roleDAO = roleDAO;
        this.userRoleDAO = userRoleDAO;
        this.userGroupDAO = userGroupDAO;
    }

    public AdminEntity get(Integer id) {
        return adminDAO.get(id);
    }

    public int create(AdminEntity entity) {
        return adminDAO.insert(entity);
    }

    public int update(AdminEntity entity) {
        return adminDAO.update(entity);
    }

    public int delete(Integer id) {
        return adminDAO.delete(id);
    }

    public int deleteByUserName(String name) {
        return adminDAO.deleteByUserName(name);
    }


    public List<AdminEntity> list() {
        return adminDAO.list();
    }

    public List<SysUserResDTO> showRolelist(SysUserQueryReqDTO vo) {
        return roleDAO.showlist(vo);
    }
    //分页查询group
    public List<UserGroupEntity> showUserGrouplist(SysGroupReqDTO vo) {
        return userGroupDAO.showGrouplist(vo);
    }

    public List<UserGroupEntity> showAllUserGrouplist() {
        return userGroupDAO.showAllGrouplist();
    }

    public long showUserGroupCount(SysGroupReqDTO vo) {
        return userGroupDAO.showGroupCount(vo);
    }

    public Integer  getRoleCount(SysUserQueryReqDTO vo) {
        return roleDAO.getRoleTotal(vo);
    }

    public RoleEntity getUser(String id) {
        return roleDAO.get(id);
    }

    public List<Map<String, String>> getUserByGroupId(String gid) {
        return roleDAO.getUserByRoleId(gid);
    }

    public long countUser(QueryWrapper<RoleEntity> queryWrapper){
        return roleDAO.count(queryWrapper);
    }
    public int createUser(RoleEntity entity){
        return roleDAO.insert(entity);
    }

    public List<RoleEntity> selectAllUsers(QueryWrapper<RoleEntity> queryWrapper){
        return roleDAO.getAllList(queryWrapper);
    }

    public int updateUser(RoleEntity entity) {
        return roleDAO.update(entity);
    }


    public int deleteUser(String id) {
        return roleDAO.delete(id);
    }

    public List<UserRoleEntity> getAllUserRoles() {
        return userRoleDAO.getAllUsers();
    }

    //groups
    public int deleteGroups(List<String> ids){
        return userGroupDAO.deleteByIds(ids);
    }

    public long countGroups(QueryWrapper q){
        return userGroupDAO.count(q);
    }

    public int saveUserGroup(UserGroupEntity input) {
        return userGroupDAO.saveUserGroup(input);

    }

    public int updateUserGroup(UserGroupEntity input) {
        return userGroupDAO.update(input);

    }
    public UserGroupEntity getGroupById (String id) {
        return userGroupDAO.get(id);
    }


    public String getGroupIdByUserName(String userName) {

        return userGroupDAO.getGroupIdByUserName(userName);
    }
}
